package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpInfoXcxShowDAO;
import org.jsola.hr.dto.EmpInfoXcxShowAddDTO;
import org.jsola.hr.dto.EmpInfoXcxShowUpdateDTO;
import org.jsola.hr.entity.EmpInfoXcxShowDO;
import org.jsola.hr.query.EmpInfoXcxShowQuery;
import org.jsola.hr.service.IEmpInfoXcxShowService;
import org.jsola.hr.vo.EmpInfoXcxShowListVO;
import org.jsola.hr.vo.EmpInfoXcxShowVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工小程序展示项
 *
 * @author zhr
 */
@Service("hrEmpInfoXcxShowServiceImpl")
public class EmpInfoXcxShowServiceImpl implements IEmpInfoXcxShowService {

    @Autowired
    private IEmpInfoXcxShowDAO empInfoXcxShowDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpInfoXcxShowVO save(EmpInfoXcxShowAddDTO empInfoXcxShowAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpInfoXcxShowDO empInfoXcxShowDO = empInfoXcxShowAddDTO.to(EmpInfoXcxShowDO.class);
        // 保存
        empInfoXcxShowDO = save(empInfoXcxShowDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empInfoXcxShowDO.to(EmpInfoXcxShowVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpInfoXcxShowUpdateDTO empInfoXcxShowUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpInfoXcxShowDO empInfoXcxShowDO = empInfoXcxShowUpdateDTO.to(EmpInfoXcxShowDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empInfoXcxShowDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empInfoXcxShowIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empInfoXcxShowIds);
    }


    @Override
    public EmpInfoXcxShowVO selectById(String empInfoXcxShowId, String siteId) {
        EmpInfoXcxShowDO empInfoXcxShowDO = selectDOById(empInfoXcxShowId, siteId);
        if (empInfoXcxShowDO == null) {
            return null;
        }
        return empInfoXcxShowDO.to(EmpInfoXcxShowVO.class);
    }

    @Override
    public List<EmpInfoXcxShowListVO> select(EmpInfoXcxShowQuery empInfoXcxShowQuery, String siteId) {
        List<EmpInfoXcxShowDO> empInfoXcxShowDOList = selectDO(empInfoXcxShowQuery, siteId);
        if(CollectionUtils.isEmpty(empInfoXcxShowDOList)) {
            return empInfoXcxShowDOList == null ? null : new ArrayList<>();
        }
        return empInfoXcxShowDOList.stream()
                .map(empInfoXcxShowDO -> empInfoXcxShowDO.to(EmpInfoXcxShowListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpInfoXcxShowQuery empInfoXcxShowQuery, String siteId) {
        Example example = buildExample(empInfoXcxShowQuery,siteId);
        return empInfoXcxShowDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpInfoXcxShowListVO> selectPage(EmpInfoXcxShowQuery empInfoXcxShowQuery, String siteId) {
        Example example = buildExample(empInfoXcxShowQuery,siteId);
        Page<EmpInfoXcxShowDO> page = empInfoXcxShowDAO.selectPageByExample(example,
                empInfoXcxShowQuery.getPageNo(),
                empInfoXcxShowQuery.getPageSize());

        return page.to(EmpInfoXcxShowListVO.class);
    }

    @Override
    public EmpInfoXcxShowDO selectDOById(String empInfoXcxShowId, String siteId) {
        return listById(empInfoXcxShowId, siteId);
    }

    @Override
    public List<EmpInfoXcxShowDO> selectDO(EmpInfoXcxShowQuery empInfoXcxShowQuery, String siteId) {
        Example example = buildExample(empInfoXcxShowQuery,siteId);
        return empInfoXcxShowDAO.selectByExample(example);
    }

    @Override
    public String[] getShowXcx(TokenUser tokenUser) {
        Example example = new Example(EmpInfoXcxShowDO.class);
        example.and().andEqualTo("siteId", "default");
        List<EmpInfoXcxShowDO> showList = empInfoXcxShowDAO.selectByExample(example);
        List<String> showCodeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(showList)) {
            showCodeList = showList.stream().filter(EmpInfoXcxShowDO::getIsShow).map(EmpInfoXcxShowDO::getModuleCode).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(showList)) {
            showCodeList.add("baseInfo");
            showCodeList.add("contactInfo");
            showCodeList.add("familyList");
            showCodeList.add("emergencyContactList");
            showCodeList.add("educationExperienceList");
            showCodeList.add("workExperienceList");
            showCodeList.add("trainingExperienceList");
            showCodeList.add("wageCardInfo");
//            showCodeList.add("appendixInfo");
        }
        return showCodeList.stream().distinct().toArray(String[]::new);
    }


    /**
     * 根据查询参数，构建example

     * @param empInfoXcxShowQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpInfoXcxShowQuery empInfoXcxShowQuery, String siteId) {
        Example example = new Example(EmpInfoXcxShowDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (empInfoXcxShowQuery.getModuleName() != null) {
            example.and().andEqualTo("moduleName", empInfoXcxShowQuery.getModuleName());
        }
        if (empInfoXcxShowQuery.getModuleCode() != null) {
            example.and().andEqualTo("moduleCode", empInfoXcxShowQuery.getModuleCode());
        }
        if (empInfoXcxShowQuery.getIsShow() != null) {
            example.and().andEqualTo("isShow", empInfoXcxShowQuery.getIsShow());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empInfoXcxShowQuery.getOrders());
        return example;
    }
}




